package org.dromara.agriculture.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.agriculture.domain.vo.AgricultureMachineInfoVo;
import org.dromara.agriculture.domain.bo.AgricultureMachineInfoBo;
import org.dromara.agriculture.service.IAgricultureMachineInfoService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 机械信息
 *
 * @author Robin
 * @date 2024-12-05
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/agriculture/machineInfo")
public class AgricultureMachineInfoController extends BaseController {

    private final IAgricultureMachineInfoService agricultureMachineInfoService;

    /**
     * 查询机械信息列表
     */
    @SaCheckPermission("agriculture:machineInfo:list")
    @GetMapping("/list")
    public TableDataInfo<AgricultureMachineInfoVo> list(AgricultureMachineInfoBo bo, PageQuery pageQuery) {
        return agricultureMachineInfoService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出机械信息列表
     */
    @SaCheckPermission("agriculture:machineInfo:export")
    @Log(title = "机械信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(AgricultureMachineInfoBo bo, HttpServletResponse response) {
        List<AgricultureMachineInfoVo> list = agricultureMachineInfoService.queryList(bo);
        ExcelUtil.exportExcel(list, "机械信息", AgricultureMachineInfoVo.class, response);
    }

    /**
     * 获取机械信息详细信息
     *
     * @param machineId 主键
     */
    @SaCheckPermission("agriculture:machineInfo:query")
    @GetMapping("/{machineId}")
    public R<AgricultureMachineInfoVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long machineId) {
        return R.ok(agricultureMachineInfoService.queryById(machineId));
    }

    /**
     * 新增机械信息
     */
    @SaCheckPermission("agriculture:machineInfo:add")
    @Log(title = "机械信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody AgricultureMachineInfoBo bo) {
        return toAjax(agricultureMachineInfoService.insertByBo(bo));
    }

    /**
     * 修改机械信息
     */
    @SaCheckPermission("agriculture:machineInfo:edit")
    @Log(title = "机械信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody AgricultureMachineInfoBo bo) {
        return toAjax(agricultureMachineInfoService.updateByBo(bo));
    }

    /**
     * 删除机械信息
     *
     * @param machineIds 主键串
     */
    @SaCheckPermission("agriculture:machineInfo:remove")
    @Log(title = "机械信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{machineIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] machineIds) {
        return toAjax(agricultureMachineInfoService.deleteWithValidByIds(List.of(machineIds), true));
    }
}
